package CutLoad.cookies
{
	import CutLoad.cookies.Fun;
	
	import flash.display.DisplayObject;
	import flash.geom.Matrix;
	import flash.geom.Point;
	/**
	 * 对可视对象，动态修改注册点进行缩放或旋转 
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class RegPointOperation
	{
		private var _obj:DisplayObject;
		private var _point:Point;
		private var initMatrix:Matrix;
		/**
		 * 初始化 
		 * @param obj 可视对象
		 * @param point 相对可视对象的注册点  注：如果为null,默认设置注册为中心
		 * 
		 */		
		public function RegPointOperation(obj:DisplayObject, point:Point=null)
		{
			_obj = obj;
			if(point==null) point=new Point();
			if(point==null){
				setRegPointCenter()
			}else{
				_point = new Point(obj.x+point.x,obj.y+point.y);
			}
			setInitMatrix();
		}
		/**
		 * 重新设置注册点 
		 * @param point
		 * 
		 */		
		public function setRegPoint(point:Point):void{
			_point=point
		}
		/**
		 * 重新设置注册点为中心点 
		 * 
		 */		
		public function setRegPointCenter():void{
			_point=new Point(_obj.x+_obj.width/2,_obj.y+_obj.height/2);
		}
		/**
		 * 获得操作的目标 
		 * @return 
		 * 
		 */		
		public function getTarget():DisplayObject{
			return _obj;
		}
		/**
		 * 把当前matrix对象设置“起始matrix”
		 * 
		 */		
		public function setInitMatrix():void{
			initMatrix=_obj.transform.matrix.clone();
		}
		/**
		 * 重设 “起始matrix”
		 * 
		 */		
		public function RestMatrix():void{
			_obj.transform.matrix=initMatrix;
		}
		/**
		 * 旋转弧度  
		 * @param r 弧度值
		 * @param isBase true:是以现在的弧度值基础之上加弧度值,false:还是设置弧度值
		 * 
		 */		
		private function rotationRadian(r:Number,isBase:Boolean=false):void
		{
			if(!isBase) RestMatrix();
			var matrix:Matrix =_obj.transform.matrix;
			matrix.translate( -_point.x, -_point.y );
			matrix.rotate(r);
			matrix.translate( _point.x, _point.y );
			_obj.transform.matrix = matrix;
		}
		/**
		 * 旋转角度 
		 * @param r 角度值
		 * @param isBase true:是以现在的角度值基础之上加角度值,false:还是设置角度值
		 * 
		 */		
		private function rotationAngle(r:Number,isBase:Boolean=false):void{
			rotationRadian(Fun.angleToRadian(r),isBase);
		}
		/**
		 * 设置 xscale和yscale值
		 * @param xscale
		 * @param yscale
		 * @return 
		 * 
		 */		
		public function setScaleXY(xscale:Number,yscale:Number):void{
			var currentX:Number=xscale
			var currentY:Number=yscale
			currentX=currentX/this.getTarget().scaleX
			currentY=currentY/this.getTarget().scaleY
			scale(xscale,yscale,false);
		}
		/**
		 * 增加或减去当前的 xscale和yscale值
		 * @param xscaleSpeed 默认为0不改变
		 * @param yscaleSpeed 默认为0不改变
		 * @return 
		 * 
		 */		
		public function addScaleXY(xscaleSpeed:Number=0,yscaleSpeed:Number=0):void{
			var currentX:Number=this.getTarget().scaleX+xscaleSpeed;
			var currentY:Number=this.getTarget().scaleY+yscaleSpeed;
			currentX=currentX/this.getTarget().scaleX
			currentY=currentY/this.getTarget().scaleY
			scale(currentX,currentY,true);
		}
		/**
		 * 缩放
		 * @param xscale 与scaleX相乘的值
		 * @param yscale 与scaleY相乘的值
		 * @param isBase 是否不设置回初始时的scaleX，scaleY   true:不设置  false:设置
		 * 
		 */
		private function scale(xscale:Number,yscale:Number,isBase:Boolean=false):void{
			if(!isBase) RestMatrix();
			var matrix:Matrix = _obj.transform.matrix;
			matrix.translate( -_point.x, -_point.y );
			matrix.scale(xscale,yscale);
			matrix.translate( _point.x, _point.y )
			_obj.transform.matrix = matrix;
		}
	}
}